/**
 * 判定五子棋是否获胜
 **/

/**
 * 判定是否获胜
 * @param checkerboardData 棋盘数据
 * @param row 横坐标
 * @param col 纵坐标
 */
export function isSuccess(checkerboardData, row, col) {
    return isHorizontalAxisSuccess(checkerboardData, row, col) || isVerticalAxisSuccess(checkerboardData, row, col)
        || isDiagonalLeftSuccess(checkerboardData, row, col) || isDiagonalRightSuccess(checkerboardData,row,col);
}

/**
 * 判定横轴是否连城五棵
 * @param checkerboardData 棋盘数据
 * @param row 横坐标
 * @param col 纵坐标
 */
function isHorizontalAxisSuccess(checkerboardData, row, col) {
    // 总个数，默认1个，因为我们刚下了一颗棋
    let count = 1;
    // 左边有几个跟他一样的, 一直向左找，找到一个颜色不一样的或者，找到最左边就结束
    for (let i = 1; col - i >= 0; i++) {
        if (checkerboardData[row][col] === checkerboardData[row][col - i]) {
            count++
        } else {
            break;
        }
    }
    for (let i = 1; col + i < 15; i++) {
        if (checkerboardData[row][col] === checkerboardData[row][col + i]) {
            count++
        } else {
            break;
        }
    }
    return count >= 5;
}

/**
 * 判定纵轴是否连城五棵
 * @param checkerboardData 棋盘数据
 * @param row 横坐标
 * @param col 纵坐标
 */
function isVerticalAxisSuccess(checkerboardData, row, col) {
    // 总个数，默认1个，因为我们刚下了一颗棋
    let count = 1;
    // 向上找
    for (let i = 1; row - i >= 0; i++) {
        if (checkerboardData[row][col] === checkerboardData[row - i][col]) {
            count++
        } else {
            break;
        }
    }
    for (let i = 1; row + i < 15; i++) {
        if (checkerboardData[row][col] === checkerboardData[row + i][col]) {
            count++
        } else {
            break;
        }
    }
    return count >= 5;
}

/**
 * 判定右斜方向是否连城五棵
 * @param checkerboardData 棋盘数据
 * @param row 横坐标
 * @param col 纵坐标
 */
function isDiagonalRightSuccess(checkerboardData, row, col) {
    let count = 1;
    // 判断上方
    for (let i = 1; row - i >= 0 && col + i < 15; i++) {
        if (checkerboardData[row][col] === checkerboardData[row - i][col + i]) {
            count++;
        } else {
            break;
        }
    }

    // 判断下方
    for (let i = 1; row + i < 15 && col - i >= 0; i++) {
        if (checkerboardData[row][col] === checkerboardData[row + i][col - i]) {
            count++;
        } else {
            break;
        }
    }

    return count >= 5;
}

/**
 * 判定左斜方向是否连城五棵
 * @param checkerboardData 棋盘数据
 * @param row 横坐标
 * @param col 纵坐标
 */
function isDiagonalLeftSuccess(checkerboardData, row, col) {
    let count = 1;
    for (let i = 1; col - i >= 0 && row - i >= 0; i++) {
        if (checkerboardData[row][col] === checkerboardData[row - i][col - i]) {
            count++;
        } else {
            break
        }
    }

    for (let i = 1; col + i < 15 && row + i < 15; i++) {
        if (checkerboardData[row][col] === checkerboardData[row + i][col + i]) {
            count++;
        } else {
            break
        }
    }
    return count >= 5
}